package com.ecarx.vhaltests.hy11.testcases.unit;

import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.provider.Settings;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.property.IProperty;
import com.ecarx.xui.adaptapi.car.property.IPropertyValue;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TimeAndDateUnitTest extends VhalUnitTestBase {

    private static final String HOURS_12 = "12";
    private static final String HOURS_24 = "24";

    static IWrapper wrapper;

    @BeforeClass
    public static void testInit() {
        wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
    }


    /**
     * 测试时间格式
     */
    @Test
    public void testTimeUnit() {
        final int propId = 0x214072ed;
        String settingsTimeFormat = Settings.System.getString(mContext.getContentResolver(), Settings.System.TIME_12_24);
        Assert.assertEquals(settingsTimeFormat, HOURS_12);

        Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, HOURS_24);

        final int[] num = {0};
        mCarPropertyMgr.registerCallback(new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                if (carPropertyValue.getPropertyId() == propId) {
                    int value = (int)carPropertyValue.getValue();
                    if (num[0] == 0) {
                        // Assert.assertEquals(value, IPropertyValue.TimeFormat.TIME_24H);
                        num[0] ++;
                    } else if (num[0] == 1){
                        // Assert.assertEquals(value, IPropertyValue.TimeFormat.TIME_12H);
                    }
                }
            }

            @Override
            public void onErrorEvent(int i, int i1) {

            }
        },propId, CarPropertyManager.SENSOR_RATE_ONCHANGE);

        Settings.System.putString(mContext.getContentResolver(), Settings.System.TIME_12_24, HOURS_12);
    }

    /**
     * 测试时间格式，Deprecated
     */
    // @Test
    // public void testTimeUnitDeprecated() {

    //     IWrapper.IPropertyId propertyIdWrapper = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IProperty.FUNC_UNIT_TIME_INDICATION);
    //     final int propId = propertyIdWrapper.getPropertyId();

    //     int timeFormat = mCarPropertyMgr.getIntProperty(propId, 0);

    //     Assert.assertEquals(IPropertyValue.TimeFormat.TIME_12H, timeFormat);

    //     mCarPropertyMgr.setIntProperty(propId, 0, IPropertyValue.TimeFormat.TIME_24H);
    //     timeFormat = mCarPropertyMgr.getIntProperty(propId, 0);

    //     Assert.assertEquals(IPropertyValue.TimeFormat.TIME_24H,timeFormat);

    //     final int[] num = {0};
    //     mCarPropertyMgr.registerCallback(new CarPropertyManager.CarPropertyEventCallback() {
    //         @Override
    //         public void onChangeEvent(CarPropertyValue carPropertyValue) {
    //             if (carPropertyValue.getPropertyId() == propId) {
    //                 int value = (int)carPropertyValue.getValue();
    //                 if (num[0] == 0) {
    //                     Assert.assertEquals(value, IPropertyValue.TimeFormat.TIME_24H);
    //                     num[0] ++;
    //                 } else if (num[0] == 1){
    //                     Assert.assertEquals(value, IPropertyValue.TimeFormat.TIME_12H);
    //                 }
    //             }
    //         }

    //         @Override
    //         public void onErrorEvent(int i, int i1) {

    //         }
    //     },propId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
    //     mCarPropertyMgr.setIntProperty(propId, 0, IPropertyValue.TimeFormat.TIME_12H);
    // }

    /**
     * 测试日期格式
     */
    @Test
    public void testDateUnit() {

        IWrapper.IPropertyId propertyIdWrapper = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IProperty.SETTING_FUNC_UNIT_DATE_INDICATION);
        final int propId = propertyIdWrapper.getPropertyId();

        int dateFormat = mCarPropertyMgr.getIntProperty(propId, 0);
        Assert.assertEquals(dateFormat, IPropertyValue.DateFormat.DATE_YMD);

        mCarPropertyMgr.setIntProperty(propId, 0, IPropertyValue.DateFormat.DATE_MDY);
        dateFormat = mCarPropertyMgr.getIntProperty(propId, 0);
        Assert.assertEquals(dateFormat, IPropertyValue.DateFormat.DATE_MDY);


        mCarPropertyMgr.setIntProperty(propId, 0, IPropertyValue.DateFormat.DATE_DMY);
        dateFormat = mCarPropertyMgr.getIntProperty(propId, 0);
        Assert.assertEquals(dateFormat, IPropertyValue.DateFormat.DATE_DMY);

        final int[] num = {0};
        mCarPropertyMgr.registerCallback(new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                if (carPropertyValue.getPropertyId() == propId) {
                    int value = (int)carPropertyValue.getValue();
                    if (num[0] == 0) {
                        Assert.assertEquals(value, IPropertyValue.DateFormat.DATE_DMY);
                        num[0] ++;
                    } else if (num[0] == 1){
                        Assert.assertEquals(value, IPropertyValue.DateFormat.DATE_YMD);
                    }
                }
            }

            @Override
            public void onErrorEvent(int i, int i1) {

            }
        },propId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        mCarPropertyMgr.setIntProperty(propId, 0, IPropertyValue.DateFormat.DATE_YMD);
    }
}